from dataclasses import dataclass
from typing import Dict
from collections import defaultdict
from abc import ABC
from enum import Enum
import json
from copy import copy


class Direction(Enum):
    """
    Direction of order/trade/position.
    """
    LONG = "多"
    SHORT = "空"
    NET = "净"

"""
# Direction无法通过json编码
dc1 = json.dumps(c1)
print(dc1)
"""

@dataclass
class Position(object):
    """
    """
    signal: str
    vt_symbol: str
    direction: Direction

    volume: int = 0
    price: float = 0
    pnl: float = 0
    data_dict = {}
    data_list = []
    time: str = ''

    def __post_init__(self):
        """"""
        self.volume = 0
        self.position_key = f"{self.vt_symbol}_{self.direction.value}"
        self.position_id = f"{self.signal}_{self.position_key}"
        self.data_dict = {}

    def to_dict(self):
        d = copy(self.__dict__)
        d["direction"] = self.direction.value
        return d

    def update_trade(self, trade):
        pass


p = Position(signal="a",vt_symbol="rb", direction=Direction.LONG)
print(p.to_dict())
print(p.__dict__)

@dataclass
class SubPosition(Position):

    sub_position_para = "sub_para"

    def __post_init__(self):
        super().__post_init__()
        self.sub_position_id = "sub_id"

    def to_dict(self):
        d = copy(self.__dict__)
        d["direction"] = self.direction.value
        return d


@dataclass
class SubPosition2(Position):

    sub_position_para = "sub_para"

    def __post_init__(self):
        super().__post_init__()
        self.sub_position_id = "sub_id"

    def to_dict(self):
        d = copy(self.__dict__)
        d["direction"] = self.direction.value
        return d

p = SubPosition(signal="a",vt_symbol="rb", direction=Direction.LONG, volume=1)
print(p.__dict__)
print(p.sub_position_para)
p.sub_position_para = "sub_para_2"
print(p.sub_position_para)

print(isinstance(p, Position))
print(isinstance(p, SubPosition))
print(isinstance(p, SubPosition2))


